package com.central.apps.handler;

import cn.hutool.core.map.MapUtil;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.bizcommon.model.flow.FlowNode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * 项目开工申请
 * projectcommencementapplicationApp
 */
@Component
public class projectcommencementapplicationHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;
    /**
     * 项目开工申请设置节点自定义审批人
     *
     * @param app
     * @param id
     * @param flowNode
     * @param vars
     * @return
     * @throws Exception
     */
    public String addFlowCandidate(String app, Long id, FlowNode flowNode, Map<String, Object> vars) throws Exception {
        //定义一个存放人员的字符串
        String executer = null;
        //查询到单据的deptcode
        List<Map<String, Object>> defectreport = handlerService.findBySql("select  deptcode from bl_projectcommencementapplication where id="+id);
        //拿到deptcode
        String deptcode = MapUtil.getStr(defectreport.get(0), "deptcode");
        //分厂设备科长节点
        if (flowNode.getId().equals("fcsbkz")) {
            List<Map<String, Object>> persons = handlerService.findBySql("select u.username from bl_person p left join sys_user u on p.personid = u.worknumber where LEFT(p.deptcode,6)= LEFT('" + deptcode + "',6)" +
                    " and position like '%设备科长%' ");
            if (CollectionUtils.isNotEmpty(persons)) {
                //人员以逗号隔开
                StringJoiner sj = new StringJoiner(",", "", "");
                for (Map<String, Object> username : persons) {
                    sj.add(MapUtils.getString(username, "username"));
                }
                executer = sj.toString();

            }
        }
        //车间主任节点
        else if (flowNode.getId().equals("cjzr")){
            List<Map<String, Object>> persons = handlerService.findBySql("select u.username from bl_person p left join sys_user u on p.personid = u.worknumber where LEFT(p.deptcode,6)= LEFT('" + deptcode + "',6)" +
                    " and position like '%车间主任%' ");
            if (CollectionUtils.isNotEmpty(persons)) {
                //人员以逗号隔开
                StringJoiner sj = new StringJoiner(",", "", "");
                for (Map<String, Object> username : persons) {
                    sj.add(MapUtils.getString(username, "username"));
                }
                executer = sj.toString();

            }
        }
        return executer;
    }
}
